<template>
  <!-- 跟进记录添加 -->
  <div class="contractInfo box">
    <el-divider content-position="left">{{
      $route.query.mode == "add" ? "添加跟进记录" : "编辑跟进记录"
    }}</el-divider>
    <div class="formBox">
      <el-form
        :model="form"
        :rules="rules"
        ref="addForm"
        label-width="auto"
        label-position="right"
      >
        <el-form-item label="客户" prop="customerId">
          <el-select
            v-model="form.customerId"
            placeholder="请选择客户"
            style="width: 100%"
            remote
            filterable
            remote-show-suffix
            :remote-method="queryCustomer"
            :loading="selectCustomerListLoading"
            clearable
            @change="updateFollowName"
          >
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.customerName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="跟进名称" prop="followName">
          <el-input
            v-model="form.followName"
            clearable
            placeholder="请输入跟进名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="跟进类型" prop="type">
          <el-select
            v-model="form.type"
            placeholder="请选择跟进类型"
            style="width: 100%"
            clearable
          >
            <el-option
              v-for="item in followTypeOptions"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="跟进内容" prop="content" style="width: 100%">
          <el-input
            v-model="form.content"
            clearable
            type="textarea"
            rows="3"
            placeholder="请输入跟进内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="跟进进度">
          <el-input
            v-model="form.progress"
            clearable
            placeholder="请输入跟进进度"
          ></el-input>
        </el-form-item>
        <el-form-item label="下次联系时间">
          <el-date-picker
            v-model="form.nextTime"
            type="datetime"
            placeholder="请选择日期和时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="附件">
          <sc-upload-file
            v-model="form.otherFileList"
            :limit="10"
            :multiple="false"
            tip="最多上传10个文件,单个文件不要超过20M"
          >
            <el-button type="primary" plain icon="el-icon-upload"
              >上传附件</el-button
            >
          </sc-upload-file>
        </el-form-item>
      </el-form>

      <div class="button">
        <el-button type="primary" @click="save" :loading="isSaveing"
          >保 存</el-button
        >
        <el-button @click="cencel">取 消</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { queryTypeByDic } from '@/utils/getData'
export default {
  name: 'customerAdd',
  data () {
    return {
      isSaveing: false,
      selectCustomerListLoading: false,
      //表单数据
      form: {
        adminId: '',//用户登录时的id
        departmentId: '',
        customerId: '',
        followName: '',//跟进名称
        content: '',//跟进内容
        progress: '',//跟进进度
        type: '',//跟进类型
        nextTime: '',//下次联系时间
        otherFileList: []
      },
      customerData: {},
      customerList: [],//客户列表
      followTypeOptions: [],
      //验证规则
      rules: {
        followName: [
          { required: true, message: '请输入跟进记录名称' }
        ],
        customerId: [
          { required: true, message: '请选择客户', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入跟进内容' }
        ],
        progress: [
          { required: true, message: '请输入跟进进度' }
        ],
        type: [
          { required: true, message: '请选择跟进类型', trigger: 'change' }
        ],
        nextTime: [
          { required: true, message: '请选择下次拜访时间', trigger: 'change' }
        ],

        userId: [
          { required: true, message: '请选择负责人', trigger: 'change' }
        ],
      }
    }
  },

  mounted () {
    this.getCustomerIdList()
    this.getType()
    if (this.$route.query.mode == 'edit') {
      this.getFollowRecordById()
    }

  },
  methods: {
    // 获取跟进类型
    async getType () {
      this.followTypeOptions = await queryTypeByDic('followType')
      this.form.type = this.followTypeOptions[0].value
    },
    updateFollowName (id) {
      this.customerList.map(item => {
        if (item.id == id) {
          this.customerData = item
        }
      })
      this.form.followName = '【' + this.customerData.customerName + '】' + '跟进记录'
    },
    // 获取客户列表
    async getCustomerIdList (val) {
      this.selectCustomerListLoading = true
      let res = await this.$API.customer.recordList.getCustomerById.post({ search: val })
      this.selectCustomerListLoading = false
      this.customerList = res.data
    },

    // 搜索客户
    queryCustomer (val) {
      this.getCustomerIdList(val)
    },

    // 获取跟进记录信息
    async getFollowRecordById () {
      let { data } = await this.$API.contract.followRecord.getDetailById.post({ id: this.$route.query.id })
      this.form = data
      this.form.type = +this.form.type

    },

    async save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let userInfo = this.$TOOL.data.get('USER_INFO')
          this.form.departmentId = userInfo.departmentId
          this.form.adminId = userInfo.userId
          this.form.customerId = this.customerData.id
          let fileType = ''
          this.form.otherFileList.map(item => {
            if (item.url) {
              let arr = item.url.split('.')
              let type = arr[arr.length - 1]
              // fileType  1图片 2文件
              let imageType = 'jpg/png/jpeg/svg'
              if (imageType.includes(type)) {
                fileType = 1
              } else {
                fileType = 2
              }
              item.fileType = fileType
              item.fileName = item.name
            }
          })
          let res = null
          let mode = this.$route.query.mode
          if (mode == 'add') {
            res = await this.$API.contract.followRecord.addFollowRecord.post(this.form)
          } else if (mode == 'edit') {
            delete this.form.createTime
            delete this.form.updateTime
            res = await this.$API.contract.followRecord.updateFollowRecord.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.closeSelectedTag(this.$route)
            this.$router.push({
              path: '/customer/followRecord'
            })
            this.$message.success("操作成功")
          }
        }
      })
    },

    // 取消添加
    cencel () {
      this.closeSelectedTag(this.$route)
      this.$router.push({
        path: '/customer/followRecord'
      })
    },
    //关闭tag
    closeSelectedTag (tag) {
      this.$store.commit("removeViewTags", tag)
      this.$store.commit("removeIframeList", tag)
      this.$store.commit("removeKeepLive", tag.name)
    },
  }

}

</script>
<style lang='scss' scoped>
.box {
  margin: 10px;
}

.formBox {
  padding: 30px;
  background: #fff;
  .el-form {
    width: 100%;
  }
}
.button {
  margin-top: 50px;
  padding-left: 60px;
}

:deep(.el-divider__text) {
  padding: 0 10px;
  font-size: 18px;
  // font-weight: 600;
  background-color: #f6f8f9;
}
</style>